const gulp = require('gulp');
const ts = require('gulp-typescript');
const cls = require('gulp-clean');
const filter = require('gulp-filter');
const uglify = require('gulp-uglify-es').default;
const es = require('event-stream');
const webpack = require('webpack');
const config = require('./webpack.config');
function clean() {
    return gulp.src(["./dist/media", "./dist/*.*"], { allowEmpty: true, read: false })
        .pipe(cls());
}
function copyhtml() {
    return gulp.src(['./media/**', '!./media/all.js'])
        .pipe(gulp.dest("./dist/media"));
}
function copyicon() {
    return gulp.src("./icon/**")
        .pipe(gulp.dest("./dist/icon"));
}
function copyalljs() {
    return gulp.src('./media/*all.js')
        .pipe(uglify())
        .pipe(gulp.dest("./dist/media"));
}
function procePackagefile() {
    return gulp.src("./package.json")
        .pipe(es.through(function (data) {
            let json = JSON.parse(data.contents.toString());
            delete json.scripts;
            delete json.devDependencies;
            delete json.dependencies;
            json.main = "./extension.js";
            data.contents = Buffer.from(JSON.stringify(json, null, '  '));
            this.emit('data', data);
        }))
        .pipe(gulp.dest('./dist'));
}

function extensionCompile(cb) {
    let compiler = webpack(config);
    compiler.run();
    compiler.hooks.done.tap("down", () => {
        cb();
    });
}
const media = gulp.parallel(procePackagefile, copyhtml, copyalljs, extensionCompile, copyicon);

exports.default = gulp.series(clean, media);
exports.webpack = extensionCompile;
exports.json = procePackagefile;
exports.clean = clean;